<?php

namespace Echecs\adminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class DefaultController extends Controller
{
    /**
     * @Route("/hello/{name}")
     * @Template()
     */
    public function indexAction()
    {
        $name='davyn';
        return $this->render('EchecsadminBundle:Admin:index.html.twig' ,array('name' => $name));
    }
    
    public function PagesAction()
    {
        $pages = $this->getDoctrine()->getRepository('EchecspageBundle:Pages:PagesRepository')->findAll();
        return $this->render('EchecsadminBundle:Admin:pages.html.twig' ,array('pages' => $pages));
    }
    
    public function CreateCategorieAction()
    {
     
        
        $request = $this->get('request');
	    $uneCat = new \Echecs\pageBundle\Entity\Categorie();
            $categorieType = new \Echecs\adminBundle\Form\Type\CategorieType();
            
	    $form = $this->createForm($categorieType,$uneCat);
	    
	    if ($request->isMethod('post'))
	    {
		    $form->bind($request);
		    
		    if($form->isValid())
		    {
			    $em = $this->getDoctrine()->getEntityManager();
			    $em->persist($uneCat);
			    $em->flush();
			    
			   return $this->render('EchecsadminBundle:Admin:success.html.twig', array('message' => 'Catégorie créée avec succès.'));
			    
		    }
		    
		    
	    }
	     
	      return $this->render('EchecsadminBundle:Admin:addCategorie.html.twig', array('form' => $form->createView()));
    }
    
    public function CreateArticleAction()
    {
     
        
        $request = $this->get('request');
	    $uneCat = new \Echecs\pageBundle\Entity\Article();
            $categorieType = new \Echecs\adminBundle\Form\Type\ArticleType();
            
	    $form = $this->createForm($categorieType,$uneCat);
	    
	    if ($request->isMethod('post'))
	    {
		    $form->bind($request);
		    
		    if($form->isValid())
		    {
			    $em = $this->getDoctrine()->getEntityManager();
                            $uneCat->setAuteurId('1');
                            $now = new \DateTime('now');
                            $uneCat->setDate($now->format('d-m-Y'));
                            $uneCat->setEtat('2');
			    $em->persist($uneCat);
			    $em->flush();
			    
			   return $this->render('EchecsadminBundle:Admin:success.html.twig', array('message' => 'Article créée avec succès.'));
			    
		    }
		    
		    
	    }
	    return $this->render('EchecsadminBundle:Admin:addArticle.html.twig', array('form' => $form->createView()));
    }
    
    public function CreatePageAction()
    {
     
        
        $request = $this->get('request');
	    $unePage = new \Echecs\pageBundle\Entity\Pages();
            $pageType = new \Echecs\adminBundle\Form\Type\PageType();
            
	    $form = $this->createForm($pageType,$unePage);
	    
	    if ($request->isMethod('post'))
	    {
		    $form->bind($request);
		    
		    if($form->isValid())
		    {
			    $em = $this->getDoctrine()->getEntityManager();
			    $em->persist($unePage);
			    $em->flush();
			    
			   return $this->render('EchecsadminBundle:Admin:success.html.twig', array('message' => 'Page créée avec succès.'));
			    
		    }
		    
		    
	    }
	    return $this->render('EchecsadminBundle:Admin:addPage.html.twig', array('form' => $form->createView()));
    }
    
     public function EditPageAction($id)
    {
     
        
            $request = $this->get('request');
	    $unePage = $this->getDoctrine()->getRepository('EchecspageBundle:Pages:PagesRepository')->find($id);

            
            $pageType = new \Echecs\adminBundle\Form\Type\PageType();
            
	    $form = $this->createForm($pageType,$unePage);
	    
	    if ($request->isMethod('post'))
	    {
		    $form->bind($request);
		    
		    if($form->isValid())
		    {

                            
			    $em = $this->getDoctrine()->getEntityManager();
			    $em->flush();
			    
			   return $this->render('EchecsadminBundle:Admin:success.html.twig', array('message' => 'Page modifiée avec succès.'));
			    
		    }
		    
		    
	    }
	    return $this->render('EchecsadminBundle:Admin:editPage.html.twig', array('page'=>$unePage, 'form' => $form->createView()));
    }
}
